package com.naiterui.ehp.bs.cms.modules.oms.controller;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.oms.service.IReturnOrderService;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ExchangeOrderDetailVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ExchangeOrderVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.LogisticsCompanyVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ReturnOrderDetailVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ReturnOrderParamVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ReturnOrderVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.SupplementaryOrderDetailVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.SupplementaryOrderVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Validated
@Api(tags = {SwaggerTag.CMS + SwaggerTag.ORDER})
@RestController
@AllArgsConstructor
@RequestMapping("oms/return/order")
public class ReturnOrderController {

  private final IReturnOrderService returnOrderService;

  @SysLog("退/换/补货列表")
  @ApiOperation("退/换/补货列表")
  @GetMapping("/list")
  @PreAuthorize("@p.check('oms:return:order:list')")
  public PageVO<ReturnOrderDetailVO> list(PageParamsVO pageParamsVO, ReturnOrderParamVO paramVO) {
    return this.returnOrderService.queryPage(pageParamsVO, paramVO);
  }

  @SysLog("退货详情")
  @ApiOperation("退货详情")
  @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "long", paramType = "path", required = true)
  @GetMapping("/detail/{orderId}")
  @PreAuthorize("@p.check('oms:return:order:detail')")
  public ReturnOrderDetailVO getReturnOrderDetail(@PathVariable("orderId") Long orderId) {
    return this.returnOrderService.getReturnOrderDetail(orderId);
  }

  @SysLog("退货订单保存")
  @ApiOperation("退货订单保存")
  @ApiImplicitParam(name = "submitFlag", value = "是否提交标记: 0/保存不提交， 1/保存并提交", dataType = "int")
  @PostMapping("/save")
  @PreAuthorize("@p.check('oms:return:order:save')")
  public void saveReturnOrder(@Validated @RequestBody ReturnOrderVO orderVO) throws BusinessException {
    log.info("保存退货订单：vo={}", orderVO.toString());
    this.returnOrderService.saveReturnOrder(orderVO);
  }


  @SysLog("换货订单详情")
  @ApiOperation("换货订单详情")
  @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "long", paramType = "path", required = true)
  @GetMapping("/exchange/{orderId}")
  @PreAuthorize("@p.check('oms:return:order:detail')")
  public ExchangeOrderDetailVO getExchangeOrderDetail(@PathVariable("orderId") Long orderId) {
    return this.returnOrderService.getExchangeOrderDetail(orderId);
  }

  @SysLog("换货订单保存")
  @ApiOperation("换货订单保存")
  @ApiImplicitParam(name = "submitFlag", value = "是否提交标记: 0/保存不提交， 1/保存并提交", dataType = "int")
  @PostMapping("/exchange/save")
  @PreAuthorize("@p.check('oms:return:order:save')")
  public void saveExchangeOrder(@Validated @RequestBody ExchangeOrderVO orderVO) throws BusinessException {
    log.info("保存换货订单：vo={}", orderVO.toString());
    this.returnOrderService.saveExchangeOrder(orderVO);
  }

  @SysLog("补寄订单详情")
  @ApiOperation("补寄订单详情")
  @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "long", paramType = "path", required = true)
  @GetMapping("/supplementary/{orderId}")
  @PreAuthorize("@p.check('oms:return:order:detail')")
  public SupplementaryOrderDetailVO getSupplementaryOrderDetail(@PathVariable("orderId") Long orderId) {
    return this.returnOrderService.getSupplementaryOrderDetail(orderId);
  }

  @SysLog("补寄订单保存")
  @ApiOperation("补寄订单保存")
  @ApiImplicitParam(name = "submitFlag", value = "是否提交标记: 0/保存不提交， 1/保存并提交", dataType = "int")
  @PostMapping("/supplementary/save")
  @PreAuthorize("@p.check('oms:return:order:save')")
  public void saveSupplementaryOrder(@Validated @RequestBody SupplementaryOrderVO orderVO) throws BusinessException {
    log.info("保存补寄订单：vo={}", orderVO.toString());
    this.returnOrderService.saveSupplementaryOrder(orderVO);
  }

  @SysLog("退/换/补寄订单审核")
  @ApiOperation("退/换/补寄订单审核")
  @ApiImplicitParam(name = "ids", value = "订单id集合", allowMultiple = true, dataType = "long", required = true)
  @PostMapping("/audit")
  @PreAuthorize("@p.check('oms:return:order:audit')")
  public void audit(@RequestBody List<Long> ids) throws BusinessException {
    log.info("退/换/补寄订单审核：ids={}", ids.toString());
    this.returnOrderService.audit(ids);
  }

  /**
   * 订单反审：退回到待提交状态
   *
   * @param ids
   * @return
   * @throws BusinessException
   */
  @SysLog("退/换/补寄订单反审")
  @ApiOperation("退/换/补寄订单反审")
  @ApiImplicitParam(name = "ids", value = "订单id集合", allowMultiple = true, dataType = "long", required = true)
  @PostMapping("/reset")
  @PreAuthorize("@p.check('oms:return:order:reset')")
  public void reset(@RequestBody List<Long> ids) throws BusinessException {
    log.info("退/换/补寄订单反审：ids={}", ids.toString());
    this.returnOrderService.reset(ids);
  }

  @SysLog("退/换/补寄订单提交")
  @ApiOperation("退/换/补寄订单提交")
  @ApiImplicitParam(name = "ids", value = "订单id集合", allowMultiple = true, dataType = "long", required = true)
  @PostMapping("/submit")
  @PreAuthorize("@p.check('oms:return:order:submit')")
  public void submit(@RequestBody List<Long> ids) throws BusinessException {
    log.info("退/换/补寄订单提交：ids={}", ids.toString());
    this.returnOrderService.submit(ids);
  }

  @SysLog("退/换/补寄订单作废")
  @ApiOperation("退/换/补寄订单作废")
  @ApiImplicitParam(name = "ids", value = "订单id集合", allowMultiple = true, dataType = "long", required = true)
  @PostMapping("/invalid")
  @PreAuthorize("@p.check('oms:return:order:invalid')")
  public void invalid(@RequestBody List<Long> ids) throws BusinessException {
    log.info("退/换/补寄订单作废：ids={}", ids.toString());
    this.returnOrderService.invalid(ids);
  }

  @SysLog("退/换/补寄订单确认收货")
  @ApiOperation("退/换/补寄订单确认收货")
  @ApiImplicitParam(name = "ids", value = "订单id集合", allowMultiple = true, dataType = "long", required = true)
  @PostMapping("/confirm")
  @PreAuthorize("@p.check('oms:return:order:confirm')")
  public void confirm(@RequestBody List<Long> ids) throws BusinessException {
    log.info("退/换/补寄订单确认收货：ids={}", ids.toString());
    this.returnOrderService.confirm(ids);
  }

  /**
   * 获取快递公司
   *
   * @return
   */
  @SysLog("获取快递公司")
  @ApiOperation("获取快递公司")
  @GetMapping("/logistics/company")
  public List<LogisticsCompanyVO> logisticsCompanyList() {
    log.info("获取快递公司");
    return this.returnOrderService.logisticsCompanyList();
  }

}
